package utils;

import java.sql.*;

/**
 * @author yicheng
 * @date 2021-08-30
 * 针对JDBS操作的工具类
 * */
public class JDBCUtils {
    static final String DRIVERNAME="com.mysql.cj.jdbc.Driver";
    static final String URL = "jdbc:mysql://127.0.0.1:3306/class?useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
    static final String USER="root";
    static final String PASSWORD="qwer1234";

    static {
        try {
            Class.forName(DRIVERNAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    static Connection connection = null;
    static PreparedStatement statement = null;
    static ResultSet resultSet = null;
    /**
    * 获取数据库连接的封装方法
    * @return Connection
    * */
    public static Connection getconnection(){
        try {
            connection = DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }
    /**
    * 查询
    * @param1  sql语句
    * @param2  sql中需要填充的参数
    */
    public static ResultSet query(String sql,Object... objs){
        //预处理
        getconnection();
        try {
            statement = connection.prepareStatement(sql);
            //判断objs是否为空或为0
            if(objs != null && objs.length > 0){
                for (int i = 0; i < objs.length; i++) {
                    statement.setObject(i+1,objs[i]);
                }
            }
            //查询操作
            resultSet = statement.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return resultSet;
    }

    /**
     * 增
     *
     * @return
     */
    public static void add(String sql){
        getconnection();
        try {
            statement = connection.prepareStatement(sql);
            System.out.println("影响行数："+ statement.executeUpdate(sql));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 删
     *
     */
    public static void delete(String sql){
        getconnection();
        try {
            statement = connection.prepareStatement(sql);
            System.out.println("executeUpdate:"+ statement.executeUpdate(sql));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }


    /**
     * 改
     *
     */
    public static void update(String sql){
        getconnection();
        try {
            statement = connection.prepareStatement(sql);
            System.out.println("executeUpdate:"+ statement.executeUpdate(sql));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    /**
     * 资源释放
     */
    public static void free(){
        if (null != resultSet){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (null != statement){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (null != connection){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
